﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 
class UserApp extends BaseApp
{
    var $type = 'user';
	var $view_scup_dir = '';

	function __construct() 
	{
      $this->UserApp();
	}
		
	function UserApp() 
	{
	   $this->view_scup_dir = ROOT_PATH.'/scup/view/'.VIEW_TYPE;   
	   parent::__construct();
	}	
	
    function index()
    {
        if(!isset($_GET['roleid']))
		{
		$_GET['roleid'] = '';
		}
        if(!isset($_GET['usertype']))
		{
		$_GET['usertype'] = '';
		}			
        if(!isset($_GET['condition']))
		{
		$_GET['condition'] = '';
		}
		$condition = $_GET['condition'];						
		$roleid = $_GET['roleid'];
		$usertype = $_GET['usertype'];
					
        if(!isset($_GET['word']))
		{
		$_GET['word'] = '';
		}					
        if(!isset($_POST['word']))
		{
		$_POST['word'] = '';
		}
		
        if(!isset($_GET['fieldtype']))
		{
		$_GET['fieldtype'] = '';
		}					
        if(!isset($_POST['fieldtype']))
		{
		$_POST['fieldtype'] = '';
		}
				
        if(!isset($_GET['v']))
		{
		$_GET['v'] = '';
		}
		if($_GET['word'])
		{
	    $word = $_GET['word'];		
		}
		if($_POST['word'])
		{
	    $word = $_POST['word'];		
		}
		
		if($_GET['fieldtype'])
		{
	    $fieldtype = $_GET['fieldtype'];		
		}
		if($_POST['fieldtype'])
		{
	    $fieldtype = $_POST['fieldtype'];		
		}		
			
		
		$word_array = explode(' ',$word);
		
		foreach($word_array as $key => $value)
		{
			if(!$value)
			{
             unset($word_array[$key]);
		    }  
		}
		 
        $order = '';
		$v = $_GET['v'];
		if($v == 'timeasc')
		{
        $order = 'reg_time asc';
		}
		else if($v == 'timedesc')
		{
        $order = 'reg_time desc';		
		}
		else if($v == 'islocked')
		{
        $order = 'locked desc';		
		}

        $temp_condition = $condition;		
        if($condition == 'lockedno')
		{
        $condition = 'locked=0';		
		}
		else if($condition == 'lockedyes')
		{
        $condition = 'locked=1';		
		}
		else if($condition == 'duetimeno')
		{
        $condition = 'due_time>'.time();		
		}
		else if($condition == 'duetimeyes')
		{
        $condition = 'due_time<'.time();		
		}
				
		$TB_role = &$this->_model('role');			
		$roledata_temp = $TB_role->selectshow();
        $roleicon = role_con(ROLE_CON_FILE);
		$roledata = array();
		$rolename = array();
		foreach($roledata_temp as $key => $value)
		{
		    $str = '';
			if(isset($value['role_name']))
			{
			     foreach($roleicon as $key1 => $value1)
				 {
				     $str .= $value1.':'.$value['role_id'].'|';
				 }
			     
				 $roledata[$value['role_id']]['name'] = $value['role_name'];
				 $roledata[$value['role_id']]['value'] = substr($str, 0, -1);
				 $rolename[$roledata[$value['role_id']]['value']] = $value['role_name'];
			}
		}
        $this->assign('rolename', $rolename);			
        $this->assign('roledata', $roledata);		
        $this->assign('roleicon', $roleicon);		

		
		$TB_usertype = &$this->_model('usertype'); 		
		$tempusertypedata = $TB_usertype->limitshow();
		$usertypedata = array();
		foreach($tempusertypedata as $key =>$value)
		{
		  $usertypedata[$value['id']]['name'] = $value['name'];
		  $usertypedata[$value['id']]['roleid'] = 'admin:'.$value['role_id'].'|scup:'.$value['role_id'];		  
		}
        $this->assign('usertypedata', $usertypedata);				
	
	    $user = & $GLOBALS['SCup']->_get_user();
	    
		$data = $user->show($order,$v,$word_array,$condition,$word,$temp_condition,$fieldtype,$roleid,$usertype);		
				
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		$this->assign('word', $word);
		$this->assign('v', $v);
		$this->assign('roleid', $roleid);		
		$this->assign('usertype', $usertype);
		$this->assign('condition', $temp_condition);		
		$this->assign('fieldtype', $fieldtype);				
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('user_list.view.php');
    }


    function post()
    {
		$data = $_POST['data'];
	    $user = & $GLOBALS['SCup']->_get_user();
	    
		$user->reg($data);
        $this->index();
    }

    function pedit()
    {
		$data = $_POST['data'];
		if(isset($data['due_time'])&&!empty($data['due_time']))
		{
		$data['due_time'] = strtotime($data['due_time']);
		}
	    $user = & $GLOBALS['SCup']->_get_user();

		$user->edit($data);
        $this->index();
    }
	
    function inipwd()
    {
		if(defined("INI_PASSWORD")&&INI_PASSWORD)
		{
        $pwd = INI_PASSWORD;		
		}else
		{
        $pwd = 'phpscup';		
		}
	    if(!isset($_POST['jobs'])){$_POST['jobs']=0;}	
		$jobs = $_POST['jobs'];
        if($jobs ==='inipwd')
		 {
		    if(!isset($_POST['checkdata'])){$_POST['checkdata']=array();}
             if(empty($_POST['checkdata']))
			 {
		    $notice = &get_message(array('msg'=>"请至少选择一个删除项目！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		    $notice ->display();				 
			 }
		    $checkdata = $_POST['checkdata'];		
			$str='';
			$pwd = md5($pwd);
			foreach($checkdata as $key => $value)
			{
			  $str.="user_id='".$value."'  or ";		  
			}			
			$str = substr($str, 0,-3);
			$db = & get_db();
		    $db->query("UPDATE ".DB_PREFIX."users SET user_passwd='{$pwd}' WHERE ".$str." ");			
		    $notice = &get_message(array('msg'=>"删除成功！",'url'=>'?app=user&act=index','title'=>'Notice'));
		    $notice ->display();				 
	         
		}
		$userid = intval($_GET['id']);
	    $user = & $GLOBALS['SCup']->_get_user();

		$user->inionepwd($userid,$pwd);
        $this->index();
    }


    function del()
    {
	    if(!isset($_POST['jobs'])){$_POST['jobs']=0;}	
		$jobs = $_POST['jobs'];
        if($jobs ==='del')
		 {
		    if(!isset($_POST['checkdata'])){$_POST['checkdata']=array();}
             if(empty($_POST['checkdata']))
			 {
		    $notice = &get_message(array('msg'=>"请至少选择一个删除项目！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		    $notice ->display();				 
			 }
		    $checkdata = $_POST['checkdata'];		
			$str='';
			foreach($checkdata as $key => $value)
			{
			  $str.="user_id='".$value."'  or ";		  
			}			
			$str = substr($str, 0,-3);
			$db = & get_db();
			$db->query("DELETE FROM ".DB_PREFIX."users WHERE "	.$str. " ");
		    $TB_usertype = &$this->_model('usertype'); 		
		    $usertypedata = $TB_usertype->limitshow();
			foreach($usertypedata as $key => $value)
			{
			$db->query("DELETE FROM ".DB_PREFIX."usertype_".$value['id']." WHERE "	.$str. " ");			
			}		
		    $notice = &get_message(array('msg'=>"删除成功！",'url'=>'?app=user&act=index','title'=>'Notice'));
		    $notice ->display();				 
	         
		}	
	
	    $id = $_GET['id'];
	    $user = & $GLOBALS['SCup']->_get_user();	    
		$user->delete($id);		  
		$this->index();	  

    }

    function batch()
    {  
	    if(!isset($_POST['jobs'])){$_POST['jobs']=0;}

	    if(!isset($_GET['selcetid'])){$_GET['selcetid']=0;}	
	    if(!isset($_POST['checkdata'])){$_POST['checkdata']=array();}
        if(empty($_POST['checkdata']))
		{
		    $notice = &get_message(array('msg'=>"请至少选择一个项目！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		    $notice ->display();				 
		}
		$checkdata = $_POST['checkdata'];
	    $str='';
		foreach($checkdata as $key => $value)
		{
			  $str.="user_id='".$value."'  or ";

		}			
		$str = substr($str, 0,-3);
		$db = & get_db();						 						
		$jobs = $_POST['jobs'];
		
        if($jobs=='movetype')
		{
		$selcetid = intval($_GET['selcetid']);

        $userdata = $db->get_all("SELECT user_id,user_name,user_type FROM ".DB_PREFIX."users WHERE ".$str." ");
	
		foreach($userdata as $key => $value)
		{ 
		  if($value['user_type'])
		  {
		  $db->query("DELETE FROM ".DB_PREFIX."usertype_".$value['user_type']." WHERE user_id=".$value['user_id']." ");		  
		  }
		  $db->query("INSERT INTO `".DB_PREFIX."usertype_".$selcetid."`(`user_id`,`user_name`) VALUES('".$value['user_id']."','".$value['user_name']."')");			
		}
						
		$db->query("UPDATE ".DB_PREFIX."users SET user_type='{$selcetid}' WHERE ".$str." ");
		$notice = &get_message(array('msg'=>"操作成功！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		$notice ->display();		 
		}		
        if($jobs=='moverole')
		{
		$selcetid = $_GET['selcetid'];
		$db->query("UPDATE ".DB_PREFIX."users SET role_id='{$selcetid}' WHERE ".$str." ");
		$notice = &get_message(array('msg'=>"操作成功！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		$notice ->display();		 
		}
        if($jobs)
		 {            
			$jobdata = del_empty_var(explode(':', $jobs));	
			$filed_key = $jobdata[0];
			$filed_value = $jobdata[1];		 
			$db->query("UPDATE ".DB_PREFIX."users SET {$filed_key}='{$filed_value}' WHERE ".$str." ");
		    $notice = &get_message(array('msg'=>"操作成功！",'url'=>'?app='.$this->type.'&act=index','title'=>'Notice'));
		    $notice ->display();	 
		}

    }

    function locked()
    {
	
	
	    $id = $_GET['id'];
		$s = $_GET['s'];		
						
	    $user = & $GLOBALS['SCup']->_get_user();

		if(empty($s))
		{						
		 $user->oneedit('locked',$id,1);
		}
		else
		{
		 $user->oneedit('locked',$id,0);		
		}
		
        $this->index();

    }	


    function add()
    {
		$TB_role = &$this->_model('role');			
		$roledata_temp = $TB_role->selectshow();
        $roleicon = role_con(ROLE_CON_FILE);
		$roledata = array();
		foreach($roledata_temp as $key => $value)
		{
		    $str = '';
			if(isset($value['role_name']))
			{
			     foreach($roleicon as $key1 => $value1)
				 {
				     $str .= $value1.':'.$value['role_id'].'|';
				 }			     
				 $roledata[$value['role_id']]['name'] = $value['role_name'];
				 $roledata[$value['role_id']]['value'] = substr($str, 0, -1);
			}
		}
		
        $this->assign('roledata', $roledata);
		
		$TB_usertype = &$this->_model('usertype'); 		
		$usertypedata = $TB_usertype->limitshow();
        $this->assign('usertypedata', $usertypedata);
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('user_add.view.php');
    }


    function kindeditor()
    {
    $w = intval($_GET['w']);
    include(ROOT_PATH."/data/ewebeditor/ewebeditor.lib.php");	
	$kindeditor = new Ewebeditor(array('imageUploadCgi'=>SITE_URL.'/admin/index.php?app='.$this->type.'&act=kindeditorupfile&w='.$w));
	$kindeditor->show();
	
    }
	
    function kindeditorupfile()
    {
    $w = intval($_GET['w']);
    include(ROOT_PATH."/data/ewebeditor/ewebeditor.lib.php");	
	$kindeditor = new Ewebeditor(array('imageUploadCgi'=>SITE_URL.'/admin/index.php?app='.$this->type.'&act=kindeditorupfile&w='.$w));
	$kindeditor->upfile($this->type.'/'.$w);	
    }
	
	
    function upfile()
    {
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	 if(!isset($_GET["edit"]))
	 {
	 $_GET["edit"]='';
	 }	 
	 if(!isset($_GET["picurl"]))
	 {
	 $_GET["picurl"]='';
	 }	
	 if(!isset($_GET['jsfun']))
	 {
	 $_GET['jsfun']='';
	 }		 
	 if(!isset($_GET['putid']))
	 {
	 $_GET['putid']='';
	 }		 
	 if(!isset($_GET['uppic']))
	 {
	 $_GET['uppic']='';
	 }	
		$tf = $_GET['tf'];
        if(empty($tf))$tf = $_POST['tf'];	 	 	
		$action = $_POST["action"];		
		$id = intval($_GET['id']);		
		$jsfun = $_GET['jsfun'];
		$uppic = $_GET['putid'];
		if(!empty($_GET['uppic']))
		{
		$uppic = $_GET['uppic'];		
		}	
		$edit = $_GET["edit"];
		$picurl = $_GET["picurl"];

        $this->assign('jsfun', $jsfun);		


        $this->assign('uppic', $uppic);		
	
				

        $this->assign('edit', $edit);		


        $this->assign('picurl', $picurl);

        $this->assign('tf', $tf);
						
		if($action == 'post')
		{
			 if(!isset($_POST['jsfun']))
	 {
	 $_POST['jsfun']='';
	 }	
	 if(!isset($_POST['uppic']))
	 {
	 $_POST['uppic']='';
	 }		 
	 if(!isset($_POST["edit"]))
	 {
	 $_POST["edit"]='';
	 }		 
	 if(!isset($_POST["picurl"]))
	 {
	 $_POST["picurl"]='';
	 }	
	 if(!isset($_POST['id']))
	 {
	 $_POST['id']='';
	 }		 
		       $jsfun = $_POST['jsfun'];
		       $uppic = $_POST['uppic'];		
		       $edit = $_POST["edit"];
		       $picurl = $_POST["picurl"];
		 	   $id || $id = intval($_POST['id']);		
		       $filedname = trim(str_replace(array("UP_"),array(""),$uppic)); 
       
		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile($this->type.'/'.$id); 	  
	           $updata = $upfile->_batch_upfile($this->type);		 
		 		 
		       $newfile = $updata[1]['path'];
	       
			   if($edit=='editp' && $picurl)
			   {

			          $TB_new = &$this->_model('content');			    		 		 		
		              $TB_new->editpicurl($id,array("$filedname"=>$newfile),$this->table_id);
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}				   
			   }

			    if($edit=='edit' && $picurl)
		       {
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}		 
		       }
		
		      $pic_url = SITE_URL . '/' . UPFILE_PATH . '/' .$newfile;

              if($tf=='pic')
			  {
	           echo "<img src='{$pic_url}' id='upsrc' width='100px' height='100px'><br/><A HREF='?app=$this->type&act=upfile&edit=$edit&tf=$tf&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改图片</A>";			  
			  }else
			  {
	           echo "<A HREF='?app=$this->type&act=upfile&edit=$edit&tf=$tf&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改文件</A>";			  
			  }

	    echo "<script>
				if(self==top){
					window.opener.$jsfun('$newfile','$uppic');
					window.self.close();
				}else{
					window.parent.$jsfun('$newfile','$uppic');
				}
		     </script>";
				
	    exit;		
		
		}

        $this->assign('id', $id);
	    $this->assign('app', $this->type);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('upfile.view.php');
    }




    function pupfile()
    {	
	 if(!isset($_GET["action"]))
	 {
	 $_GET["action"]='';
	 }
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	        if(!isset($_POST['id']))
	        {
	          $_POST['id']='';
	        }  	
		$action = $_GET["action"];			
		$action || $action = $_POST["action"];
		
		$id = intval($_GET['id']);
		$cid =$id;
		$tf = $_GET['tf'];
        if(empty($tf))$tf = $_POST['tf'];	
						
		if($action == 'post')
		{
		
		 	   $id || $id = intval($_POST['id']);		
               $cid = $id;
		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile($this->type.'/'.$id); 	  
	           $updata = $upfile->_batch_upfile($this->type);		 
		 		 
		       $newfile = $updata[1]['path'];
			   
               unset($data);
		       $data['uid'] = $GLOBALS['SCup']->SCup_user_msg['user_id'];		       
               $data['con_id'] = $id;
               $data['appname'] = $this->type;			   
			   $data['file_url'] = $updata[1]['path'];
			   $data['size'] = $updata[1]['size'];
			   $data['type'] = $updata[1]['type'];
			   			   
			   $TB_new = &$this->_model('upfile');			    		 		 		
		       $TB_new->post($data);
					   	   
	    }
		else if($action == 'del')
		{
			   $id || $id = intval($_POST['id']);   
			   $TB_new = &$this->_model('upfile');
			   
			    $picdata = $TB_new->oneshow($id);
				$picurl = $picdata['file_url'];
                $cid = $picdata['con_id'];
			   			    		 		 		
		       $TB_new->del($id);
					  
		       $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}			
		
		}			

	    $TB_new = &$this->_model('upfile');

        $this->assign('picdata', $TB_new ->selct($cid,$this->type));		
        $this->assign('tf', $tf);	
        $this->assign('id', $cid);
	    $this->assign('app', $this->type);					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('pupfile.view.php');
    }

	

 
     function edit()
    {
	    $id = $_GET['id'];
	    $user = & $GLOBALS['SCup']->_get_user();
	    
		$data = $user->get_info($id , 'user_id');

		$data['due_time'] = date("Y-m-d G:i:s", $data['due_time']);

		$TB_role = &$this->_model('role');			
		$roledata_temp = $TB_role->selectshow();
        $roleicon = role_con(ROLE_CON_FILE);
		$roledata = array();
		foreach($roledata_temp as $key => $value)
		{
		    $str = '';
			if(isset($value['role_name']))
			{
			     foreach($roleicon as $key1 => $value1)
				 {
				     $str .= $value1.':'.$value['role_id'].'|';
				 }			     
				 $roledata[$value['role_id']]['name'] = $value['role_name'];
				 $roledata[$value['role_id']]['value'] = substr($str, 0, -1);
			}
		}
		
		$TB_usertype = &$this->_model('usertype'); 		
		$usertypedata = $TB_usertype->limitshow();
        $this->assign('usertypedata', $usertypedata);
		
        $this->assign('roledata', $roledata);
        $this->assign('data', $data);
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('user_edit.view.php');
    }
 
    function filedlist()
    {
		
		$data = $this->get_userfiled_data();
		
		$field_config = unserialize($data[1]['config']);
		
        $this->assign('fieldconfig', $field_config);		
 
        $this->assign('id', $id);	   
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('user_filedlist.view.php');
    }
	
    function filedadd()
    {	

        $sxarray1 = get_all_dirname(ROOT_PATH . '/data/sx');
		$sxarray = array();
	    foreach($sxarray1  as $key => $value)
		{
		   if(strpos($value,'.php') && strstr($value,'sx_'))
		   {
		      $strid = str_replace(array('.php','sx_'),array('',''),$value);
			  $tempname = $this->get_sx_cache($strid); 
		      $sxarray[$tempname] = $value;
		   }
		}		

        $this->assign('sxarray', $sxarray);
			
		$mdivdata = $this->get_module_data();

        $this->assign('mdivdata', $mdivdata);
					
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('user_filedadd.view.php');
    }

    function filededit()
    {	
        $id = intval($_GET['id']);	
        $tbid = intval($_GET['tbid']);

        $sxarray1 = get_all_dirname(ROOT_PATH . '/data/sx');
		$sxarray = array();
	    foreach($sxarray1  as $key => $value)
		{
		   if(strpos($value,'.php') && strstr($value,'sx_'))
		   {
		      $strid = str_replace(array('.php','sx_'),array('',''),$value);
			  $tempname = $this->get_sx_cache($strid); 
		      $sxarray[$tempname] = $value;
		   }
		}		

        $this->assign('sxarray', $sxarray);
         $field_one_info = $this->get_userfiled_data();
	     $field_config_data = unserialize($field_one_info[1]['config']);
	     $data = $field_config_data[$tbid];
		 if(empty($data)||!is_array($data))
		 {
           $data = array();
		 }

        $this->assign('data', $data);			
			
		$mdivdata = $this->get_module_data();
        $this->assign('mdivdata', $mdivdata);
	
        $this->assign('id', $id);
        $this->assign('tbid', $tbid);						
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('user_filededit.view.php');
    }

   function filedpostedit()
    {

         $id = intval($_POST['id']);
         $tbid = intval($_POST['tbid']);
	     $data = $_POST['data'];
		 $tempdata = $temptype = '';
		 $temptype = $data['form']['type'];
		 $tempdata = $data['form'][$data['form']['type']];
		 unset($data['form']);
		 $data['form']['type'] = $temptype;
		 $data['form']['icon'] = $data['filed']['cname'];			 
		 $data['form'][$data['form']['type']] = $tempdata;
		 $data['form'][$data['form']['type']]['name'] = 'data['.$data['filed']['name'].']';		 
		 $data['form']['sx_data'] = $data['sx_data'];
		 unset($data['sx_data']);

	    if(!preg_match("/^([a-z])([a-z0-9_]{2,})$/i",$data['filed']['name']))
		{	
		 $notice = &get_message(array('msg'=>'数据库字段名不符合规则','url'=>'','title'=>'Notice'));
		 $notice ->display();		 
	    }

		 $db = & get_db();	
		 	 
/*		 if($db->check_fields(DB_PREFIX."content_{$id}",array($data['filed']['name']=>'0')))
		 {
		 $notice = &get_message(array('msg'=>'此数据库字段名已存在,请更换一个！','url'=>'','title'=>'Notice'));
		 $notice ->display();			 
		 }
*/



         $field_one_info = $this->get_userfiled_data();
		 
	     $field_config_data = unserialize($field_one_info[1]['config']);

		 if(empty($field_config_data)||!is_array($field_config_data))
		 {
           $field_config_data = array();
		 }

		 
		 $filedstr = $this->get_field_str($data['filed']['name'],$data['filed']['type'],$data['filed']['length'],$data['filed']['isnull'],$data['filed']['defaultv'],'', $data['tips'],false);		 	
		 $db->query("ALTER TABLE `".DB_PREFIX."users` CHANGE `".$field_config_data[$tbid]['filed']['name']."` ".$filedstr);	 
		 		 
		 
         $field_config_data[$tbid] = $data;	
		 unset($dataconfig);		 
		 $dataconfig[1]['config'] = serialize($field_config_data);
		 $dataconfig[1]['id'] = '1';
         $this->write_userfiled_cache($dataconfig);
	     $this->produced_html();		 		 		 
	     $this->filedlist();


    }


    function filedpostadd()
    {

	      $data= $_POST['data'];
		 

		 $tempdata = $temptype = '';
		 $temptype = $data['form']['type'];
		 $tempdata = $data['form'][$data['form']['type']];
		 unset($data['form']);
		 $data['form']['type'] = $temptype;
		 $data['form']['icon'] = $data['filed']['cname'];			 
		 $data['form'][$data['form']['type']] = $tempdata;
		 $data['form'][$data['form']['type']]['name'] = 'data['.$data['filed']['name'].']';	
		 $data['form']['sx_data'] = $data['sx_data'];
		 unset($data['sx_data']);

	 
	    if(!preg_match("/^([a-z])([a-z0-9_]{2,})$/i",$data['filed']['name']))
		{	
		 $notice = &get_message(array('msg'=>'数据库字段名不符合规则','url'=>'','title'=>'Notice'));
		 $notice ->display();		 
	    }

		 $db = & get_db();		 
		 if($db->check_fields(DB_PREFIX."users",array($data['filed']['name']=>'0')))
		 {
		 $notice = &get_message(array('msg'=>'此数据库字段名已存在,请更换一个！','url'=>'','title'=>'Notice'));
		 $notice ->display();			 
		 }
		 $filedstr = $this->get_field_str($data['filed']['name'],$data['filed']['type'],$data['filed']['length'],$data['filed']['isnull'],$data['filed']['defaultv'],'', $data['tips'],false);

		 $db->query("ALTER TABLE `".DB_PREFIX."users` ADD ".$filedstr);	
	 
         $field_one_info = $this->get_userfiled_data();
		 
	     $field_config_data = unserialize($field_one_info[1]['config']);
 
		 if(empty($field_config_data)||!is_array($field_config_data))
		 {
           $field_config_data = array();
		 }
		 	 
		 array_push($field_config_data,$data);
		 unset($dataconfig);	 
		 $dataconfig[1]['config'] = serialize($field_config_data);
		 $dataconfig[1]['id'] = '1';		 		 
         $this->write_userfiled_cache($dataconfig);
	     $this->produced_html();		 		 		 
	     $this->filedlist();


    }

   function fileddel()
    {

         $id = intval($_GET['id']);
         $tbid = intval($_GET['tbid']);
         $field_one_info = $this->get_userfiled_data();
		 
	     $field_config_data = unserialize($field_one_info[1]['config']);
 
		 if(empty($field_config_data)||!is_array($field_config_data))
		 {
           $field_config_data = array();
		 }
		 $db = & get_db();		 
		 $db->query("ALTER TABLE `".DB_PREFIX."users` DROP `".$field_config_data[$tbid]['filed']['name']."` ");	 
		 
         if(isset($field_config_data[$tbid]))
		 {
		 unset($field_config_data[$tbid]);
		 } 
		 
		 unset($dataconfig);		 
		 $dataconfig[1]['config'] = serialize($field_config_data);
		 $dataconfig[1]['id'] = '1';
         $this->write_userfiled_cache($dataconfig);
	     $this->produced_html();		 		 		 
	     $this->filedlist();

    }

    function produced_html($yes='no')
    {	

		$view_admin_edit_file=$GLOBALS['SCup']->SCup_view_root."/user_edit.view.php";

		$view_scup_edit_file=$this->view_scup_dir."/user/user.view.html";
				
	    $view_admin_edit=read_file($view_admin_edit_file);			
	    $view_scup_edit=read_file($view_scup_edit_file);
			
        include_once(ROOT_PATH . '/lib/div.lib.php');
		$phpscupdiv= new PhpSCup_div('1','user','userfiled/user_list.php');
        $phpscupdiv_array = $phpscupdiv->get_phpscupdiv('1','user','userfiled/user_list.php');
		$phpscupdiv_temp_array = array();
        foreach($phpscupdiv_array as $key =>$value)
		{
		 $phpscupdiv_temp_array[$value['icon']] = str_replace(array("&lt;","&gt;"),array("<",">"),$value['phpdiv']);
		} 
        $phpscuplistarray = $phpscupdiv->get_module_list_array('1','userfiled/user_list.php');	
			
        $view_admin_edit = $phpscupdiv->_get_content_html($view_admin_edit,$phpscupdiv_temp_array);		
        $view_scup_edit = $phpscupdiv->_get_content_html($view_scup_edit,$phpscupdiv_temp_array);		
			
	    write_file($view_admin_edit_file,$view_admin_edit);
	    write_file($view_scup_edit_file,$view_scup_edit);
				
   	    if(!is_writable($view_admin_edit_file))
		{	
		 $notice = &get_message(array('msg'=>$view_admin_edit_file."模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($view_scup_edit_file))
		{	
		 $notice = &get_message(array('msg'=>$view_scup_edit_file."模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }

		 if($yes=='no')
		 {
		 $notice = &get_message(array('msg'=>"模块修改成功!",'url'=>'?app=cache','title'=>'Notice'));
		 $notice ->display();
		 }
    }
	
	function get_module_data() 
	{
		return include (ROOT_PATH . '/data/module/module_div.php');
	}
	
	function get_userfiled_data() 
	{
		return include (ROOT_PATH . '/data/userfiled/user_list.php');
	}
	 
    function write_userfiled_cache($data=array())
    { 
		$cache_filename = ROOT_PATH . '/data/userfiled/user_list.php';
		
	    write_file($cache_filename, "<?php \nreturn " . var_export($data , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
    }	 

    function get_field_str($name,$type,$length = '',$isnull = false,$default = '',$extra = '', $comment = '',$default_orig = false)
    {

        $is_timestamp = strpos(' ' . strtoupper($type), 'TIMESTAMP') == 1;

        $query = $this->str_backquote($name) . ' ' . $type;

        if ($length != '' && !preg_match('@^(data|datetime|time|tinyblob|tinytext|blob|text|mediumblob|mediumtext|longblob|longtext)$@i', $type)) 
		{
            $query .= '(' . $length . ')';
        }
        if ($isnull !== false) 
		{
            if (!empty($isnull)) 
			{
                $query .= ' NOT NULL';
            } else 
			{
                $query .= ' NULL';
            }
        }
		if ($extra !== 'AUTO_INCREMENT' && (strlen($default) || $default != $default_orig)) 
		{
            if (strtoupper($default) == 'NULL') 
			{
                $query .= ' DEFAULT NULL';
            } else 
			{
                if (strlen($default)) 
				{
                    if ($is_timestamp) 
					{
                        $query .= ' DEFAULT ' . $this->str_sqlAddslashes($default);
                    } else 
					{
                        $query .= ' DEFAULT \'' . $this->str_sqlAddslashes($default) . '\'';
                    }
                }
            }
        }

        if (!empty($extra)) 
		{
            $query .= ' ' . $extra;

        }
		
        if (get_db()->version() >= '4.1' && !empty($comment)) 
		{
            $query .= " COMMENT '" . $this->str_sqlAddslashes($comment) . "'";
        }
        return $query;
    } 

    function str_backquote($name)
    {
        if (strlen($name) && $name !== '*') 
		{
            return '`' . str_replace('`', '``', $name) . '`';
        } else 
		{
            return $name;
        }
    } 

    function str_sqlAddslashes($string = '')
    {
        $string = str_replace('\\', '\\\\', $string);
        $string = str_replace('\'', '\'\'', $string);
        return $string;
    }
		


    function get_sx_cache($id)
    {

        $data = include(ROOT_PATH.'/data/sx/sx_'.$id.'.php');  
		return $data['name'];
    } 

    function power()
    {
		
	    $id = $_GET['id'];
		$pname = $_GET['v'];
	    $user = & $GLOBALS['SCup']->_get_user();
		$userdata = $user->get_info($id , 'user_id');
		$roledata = $userdata['config'];
		$roledata = @unserialize($roledata);
		
		if(!isset($roledata['powerdb']))$roledata['powerdb']=array();
				
		unset($GLOBALS['roledata_powerdb']);
		$GLOBALS['roledata_powerdb'] = $roledata['powerdb'];
		
        $this->assign('pname', $pname); 	
        $this->assign('userid', $id);

        $this->assign('username', $userdata['user_name']);
		     
        $this->assign('powerdata', $this->get_powerdata($pname));

        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('user_power_list.view.php');
    }


    function powerpost()
    {
		if(!isset($_POST['id']))
		{
		    $_POST['id']='';
		}
		if(!isset($_POST['power']))
		{
		    $_POST['power']='';
		}		
		
		if(!isset($_POST['v']))
		{
		    $_POST['v']='';
		}			
	    $id = $_POST['id'];
        $power = $_POST['power'];
		$pname = $_POST['v'];
		
		$powerdata = array();


		if(empty($power))
		{
		  $power = array();
		}

		foreach($power as $key => $value)
		{
		  $str_array = explode('|',$value);
		  $key1 = $str_array[0];
		  $value1 =  explode(':',$str_array[1]);
		  if(!isset($str_array[2]))
		  {
		  $str_array[2] = '0';
		  }
		  
		      foreach($value1 as $key2 => $value2)
			  {			      
			      if($value2)
				  {
				      if($str_array[2])
					  {
				      $powerdata[$key1.':0'][$value2] = array('set' => 1,'url'=>''); 					  
					  }
				      $powerdata[$key1.':'.$str_array[2]][$value2] = array('set' => 1,'url'=>''); 
				  }
			      
			  }		  
		}

/*       $powerdata_temp = array();

        $powerdata_temp = $this->get_powerdata($pname);
  
		foreach($powerdata_temp as $key => $value)
		{

              
		      foreach($value['act'] as $key2 => $value2)
			  {
			  
			  		$temp_array =  explode(':',$key2);
		            foreach($temp_array as $key3 => $value3)
			        {
			              if($value3&&(!isset($powerdata[$value['app']][$value3])||!isset($powerdata[$value['app']][$value3]['set'])))
				          {
				           $powerdata[$value['app']][$value3] = array('set' => 0,'url'=>''); 
				          }
			      
			        }		 

			      
			  }		  
		}
*/

	    $user = & $GLOBALS['SCup']->_get_user();
		$userdata = $user->get_info($id , 'user_id');
		$roledata = $userdata['config'];
		
		 $roledata = @unserialize($roledata);
		 if(!isset($roledata['powerdb']))$roledata['powerdb']=array();

		 if(!isset($roledata['powerdb'][$pname]))$roledata['powerdb'][$pname]=array();

		 $roledata['powerdb'][$pname] = $powerdata;
	 
		 $roledata = serialize($roledata);

         $user->oneedit('config',$id,$roledata);

		 $this->index();		 


    }


	
	
	
    function get_powerdata($name)
    {
         
        $default_filename = ROOT_PATH . '/lib/power/'.$name.'.power.php';  
		$filename = ROOT_PATH . '/data/power/data/'.$name.'.power.php'; 			 
      //  if((file_exists($default_filename) && filemtime($default_filename) > filemtime($filename)) )
		//{
		  $this->write_power_cache();
		
		//}

		if(is_file($filename))
	    {
	     $powerdata = include($filename); 
		}
		else
	    {
          exit('Missing PowerFile');
	    } 		
        return $powerdata;

    }	


    function write_power_cache()
    { 
		$TB_module = &$this->_model('module'); 		
		$infos = $TB_module->show();
		$infos_temp = array();		
		array_var_sort($infos,'sort');		
		foreach($infos as $key => $value)
		{
		   $infos_temp[$value['appname']] = $value;
		}	
		
		$default_adminpower = include(ROOT_PATH .'/lib/power/admin.power.php');
		$default_scuppower = include(ROOT_PATH .'/lib/power/scup.power.php');


		foreach($infos_temp as $key=>$value)
		{
           $catdata = $GLOBALS['SCup']->_get_category_data($value["appname"]);
		   if(!isset($catdata[0]['cat_id']))
		   {
               $catdata =   array (
                                     0 => 
                                        array (
                                              'cat_id' => '0',
                                              'cat_name' => '',

                                              ),
                                  );		   
		   }		
		   foreach($catdata as $key1=>$value1)
		   {
		   
	   $default_adminpower[] = array 
       ('name' => $value["name"].'----'.$value1['cat_name'],
	    'app' => $value["appname"],
	    'catid' => $value1['cat_id'],		
		'act' => array 
                     ('index'=>'查看列表',
					  'add:addpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'添加',	
					  'del'=>'删除',
					  'edit:editpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'修改',
					  'cagrylist'=>'分类列表',
					  'cagryadd:cyaddpost'=>'分类添加',	
					  'delcatgy'=>'分类删除',	
					  'editcatgy:cyeditpost'=>'分类修改',	
					  'hui'=>'回复管理',	
					  'batch'=>'批量操作',						  					  																			
                     )
       );
	   $default_scuppower[] = array 
       ('name' => $value["alias"].'----'.$value1['cat_name'],
	    'app' => $value["appname"],
	    'catid' => $value1['cat_id'],			
		'act' => array 
                     ('index'=>'列表',
					  'show'=>'内容显示',	
					  'userlist'=>'用户中心内容列表',						  
					  'add:addpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'添加',	
					  'del'=>'删除',
					  'edit:editpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'修改',
					  'hui'=>'回复管理',						  					  											
                     )
       );		   
		   
		   }



		}


		$cache_filename_admin = ROOT_PATH .'/data/power/data/admin.power.php';
		$cache_filename_scup = ROOT_PATH .'/data/power/data/scup.power.php';	
		
	    write_file($cache_filename_admin, "<?php \nreturn " . var_export($default_adminpower , true) . ";\n?>");
	    write_file($cache_filename_scup, "<?php \nreturn " . var_export($default_scuppower , true) . ";\n?>");
		
		if(!is_writable($cache_filename_admin)||!is_writable($cache_filename_scup))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
    }	
		

  
}

    function role_con($str)
    {
	      $array = array();
          $str_array = explode('|',$str);
	      foreach($str_array as $key => $value)
	      {
	            if($value)
		        {
			   
			   $array[$key] = $value;
		   
		        }
	 
	      }    
	   
	      return $array; 		

    }


function powercheck($app,$act,$scup,$catid=0)
{

		$str_array =  explode(':',$act);
		
		foreach($str_array as $key => $value)
		{         
		          if(!isset($GLOBALS['roledata_powerdb'][$scup][$app.':'.$catid][$value]['set']))
				  {
				  $GLOBALS['roledata_powerdb'][$scup][$app.':'.$catid][$value]['set']='';
				  }
			      if($value && $GLOBALS['roledata_powerdb'][$scup][$app.':'.$catid][$value]['set']==1)
				  {
                   	return true;			   
				  }
			      
		}		  

        return false;

}

?>